<?php
/**
 * @title 34. 在排序数组中查找元素的第一个和最后一个位置 - 二分+递归
 * @author start2004
 */

class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $target
     * @return Integer[]
     */
    function searchRange($nums, $target) {
        $len = count($nums);
        $left = 0;
        $right = $len-1;

        /**
         * @since 2020-07-05 可以先2分, 确定一个较小的范围再查找
         * @since 2020-07-05 从左开始, 第一个出现target的位置
         */
        $rangeLeft = self::find($nums, $left, $right, $target, "left");
        if($rangeLeft == -1){
            /**
             * @return
             */
            return [-1, -1];
        } else {
            /**
             * @since 2020-07-05 向右找, 从rangeleft开始, 不可能-1
             */
            $rangeRight = self::find($nums, $rangeLeft, $right, $target, "right");

            /**
             * @return
             */
            return [$rangeLeft, $rangeRight];
        }
    }

    /**
     * @title 从左/右开始, 第一个出现target的位置
     * @author start2004
     * @since 2020-07-05 Created
     *
     * @param array $nums
     * @param int $left
     * @param int $right
     * @param int $target
     * @param string $direction
     */
    function find($nums = [], $left = 0, $right = 0, $target = 0, $direction = "left"){
        $targetPosition = null;
        while($left <= $right){
            // $mid = intval(($left+$right)/2);
            $mid = ($left+$right)>>1;
            $midVal = $nums[$mid];

            /**
             * @since 2020-07-05 找到target
             */
            if($target == $midVal){
                $targetPosition = $mid;
                break;
            } else {}

            /**
             * @since 2020-07-05 重新确定区间
             */
            if($target < $midVal){
                $right = $mid-1;
            } else {
                $left = $mid+1;
            }
        }

        /**
         * @since 2020-07-05 找不到该元素
         */
        if($targetPosition === null){
            return -1;
        } else {
            /**
             * @since 2020-07-05 找到该元素
             */
            if($direction == "left"){
                /**
                 * @since 2020-07-05 继续向左找
                 */
                $right = $mid-1;
            } else {
                /**
                 * @since 2020-07-05 向右找
                 */
                $left = $mid+1;
            }
            $position = self::find($nums, $left, $right, $target, $direction);
            if($position == -1){
                return $mid;
            } else {
                return $position;
            }
        }
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.04_5.php
 */
$datas = [
    [[5,7,7,8,8,10], 8],
    [[5,7,7,8,8,10], 6],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->searchRange($data[0], $data[1]);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
